﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class BookingModule
    {
        private readonly BookingDAO bookingDAO = new BookingDAO();

        public List<CheckIn> SelectCheckingHistory(int cusID)
        {
            return bookingDAO.SelectCheckingHistory(cusID);
        }

        public List<vTotalCheck> SelectTotalCheck(DateTime date, int check)
        {
            return bookingDAO.SelectTotalCheck(date, check);
        }

        public bool CreateBooking(int pCustomerID, List<int> roomId, DateTime checkInDate, DateTime checkOutDate,
                                  string employeeAccount, string employeePassword)
        {
            checkInDate = checkInDate.Date;
            checkOutDate = checkOutDate.Date;
            if (checkInDate >= checkOutDate)
                throw new Exception("CheckIn date must be earlier than CheckOut date");

            string role = new AuthenDAO().RoleGet(employeeAccount, employeePassword);
            if (role == "Recipient" || role == "Web")
            {
                checkInDate = checkInDate.Add(new TimeSpan(12, 0, 0));
                checkOutDate = checkOutDate.Add(new TimeSpan(11, 0, 0));


                List<int> list = new SearchDAO().AvailableRoomIDs(checkInDate, checkOutDate);

                if (roomId.All(list.Contains))
                {
                    var booking = new Booking
                                      {
                                          CustomerID = pCustomerID,
                                          BookingDate = DateTime.Today
                                      };
                    List<CheckIn> lstCheckIn = roomId.Select(rID => new CheckIn
                                                                        {
                                                                            RoomID = rID,
                                                                            CheckInDate = checkInDate,
                                                                            CheckOutDate = checkOutDate,
                                                                            Available = false,
                                                                        }).ToList();

                    return bookingDAO.InsertBooking(booking, lstCheckIn);
                }
                throw new Exception("Valid Room list");
            }

            throw new Exception("Permition deny");
        }

        public bool DeleteBooking(int pBookingId)
        {
            return bookingDAO.DeleteBooking(pBookingId);
        }

        public List<Booking> GetBookingByCustomerId(int customerId)
        {
            return bookingDAO.SelectBookingByCustomerId(customerId);
        }

        public Booking GetBooking(int bookingId)
        {
            return bookingDAO.SelectBooking(bookingId);
        }
    }
}